# -*- coding:utf-8 -*-
# @Author: komeiji satori
from sanic import Sanic, request, response
import aiofiles

app = Sanic("sanic_service")


@app.route("/login", methods=["GET", "POST"])
async def login(request: request.Request):
    # 如果是 get 请求直接渲染表单页面
    if request.method == "GET":
        return response.html(
            await (await aiofiles.open("login.html", encoding="utf-8")).read()
        )
    # 如果是 post 请求, 则获取表单参数
    elif request.method == "POST":
        # 我们可以通过 request.form 拿到请求的表单内容, 返回的也是一个 RequestParameters 对象
        # 如果多个 input 标签设置了相同的 name, 那么也可以通过 getlist 获取一个列表
        # 不过这种情况不多见
        username = request.form.get("username")
        password = request.form.get("password")
        print(username)
        if username == "hanko" and password == "hansongchao":
            return response.html("<h3>欢迎来到XX小屋</h3>")
        else:
            return response.html("<h3>用户名或密码错误</h3>")


if __name__ == "__main__":
    # sanic index.app --dev
    app.run()
